/*
This file is part of La Linea.

La Linea is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

La Linea is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with La Linea.  If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once
#ifndef ANIMATION_H
#define ANIMATION_H

#include "globals.h"

#include "CacheManager.h"
#include "Frame.h"

#include <algorithm>
#include <deque>
#include <fstream>
#include <iostream>
#include <string>

class Animation
{
private:
	unsigned int _currentFrame;
	float _frameDuration;
	float _clock;
	std::string _name;
	std::deque<Frame*> _frames;

public:
	Animation(std::string name);
	~Animation(void);

	// Draw the current Frame
	void draw(GLfloat colorr = 1.0, GLfloat colorg = 1.0, GLfloat colorb = 1.0, GLfloat colora = 1.0, unsigned int options = DRAW_BORDERLINE);

	// Go to the next Frame
	void nextFrame();

	// Go to the previous Frame
	void previousFrame();

	// Update the animation
	void update(float dt);

	// Update the framerate (e.g. : 0.3 = one Frame every 0.3 seconds)
	void setFrameDuration(float f);

	// Get the framerate
	float getFrameDuration();
	
	// Get Animation's name
	const std::string& getName() const;

	// Add the Frame at the position pos
	// If pos == -1, add the frame at the end of the animation
	void addFrame(Frame* f, int pos = -1);

	// Get the current Frame
	Frame* getCurrentFrame();

	// Replace the current Frame
	void replaceCurrentFrame(Frame* f);

	// Get the total number of Frames
	unsigned int getFrameNumber();

	// Get the current frame's number
	unsigned int getCurrentFrameNumber();
	
	// Set the current frame's number
	void setCurrentFrameNumber(unsigned int i);

	// Get the Frame #i
	Frame* getFrame(unsigned int i);

	// Invalidate every frame in the animation
	void invalidateFrames();

	// Save the animation
	void saveAnim(const char* path);

	// Save the stencils
	void saveStencils(const char* path);
	
	// Load an animation
	bool loadAnim(const char* path);
	
	// Load stencils
	bool loadStencils(const char* path);
	
	// Get the nearest node
	Node* nearestNode(Vector2f pos);
	
	// Get the nearest origin point
	Node* nearestOriginNode(Vector2f pos);
	
	// Add a point to the scene. Its origin can be specified
	void addPoint(Vector2f posSouris, Node* origin = 0);
	
	// Add a positive stencil to the current frame
	void addPosStencil(FrameStencil* s);
	
	// Add a negative stencil to the current frame
	void addNegStencil(FrameStencil* s);
	
	// Remove the current frame
	void removeFrame();
};

#endif
